﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            byte[] randBytes = OpenSSL.Core.Random.Bytes(16);   //获取16字节（128位）的随机值
            // 转16进制字符串 4a89c1e660558cad1ffc6ff83882c1f7
            string seed128 = ByteToHexStr(randBytes);
            // base64url 加密 
            // SonB5mBVjK0f/G/4OILB9w
            char[] padding = { '=' }; //去掉==
            string encrypt_seed128 = Convert.ToBase64String(randBytes).TrimEnd(padding).Replace('+', '-').Replace('/', '_');   


            string seed32 = seed128.Substring(0, 8);
            Int64 seed32_10 = Convert.ToInt64(seed32, 16);
            string passcode_str = (Convert.ToDecimal(seed32_10) / 100000000).ToString().Split('.')[1];
            // passcode 8位数 12345678
            string passcode = Convert.ToInt32(passcode_str).ToString("00000000");






        }


        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("x2");
                }
            }
            return  returnStr;
        }

        /// <summary>
        /// 字节数组转16进制字符串：空格分隔
        /// </summary>
        /// <param name="byteDatas"></param>
        /// <returns></returns>
        public static string ToHexStrFromByte(byte[] byteDatas)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < byteDatas.Length; i++)
            {
                builder.Append(string.Format("{0:x2} ", byteDatas[i]));
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 将16进制的字符串转为byte[]
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] StrToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

    }
}
